/**判断是不是json */
export function isJSONString(str: any): boolean {
    if (typeof str == 'string') {
        try {
            var obj = JSON.parse(str);
            if (typeof obj == 'object' && obj) {
                return true;
            } else {
                return false;
            }

        } catch (e) {
            return false;
        }
    }
    return false
}

// 生成随机数
export const getUUID = function (len?: number) {
    len = len || 6
    len = parseInt(len.toString(), 10)
    len = isNaN(len) ? 6 : len
    var seed = '0123456789abcdefghijklmnopqrstubwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ'
    var seedLen = seed.length - 1
    var uuid = ''
    while (len--) {
      uuid += seed[Math.round(Math.random() * seedLen)]
    }
    return uuid
}
/*
 * @Author: tangyuhui 317972442@qq.com
 * @Date: 2025-09-10 14:34:18
 * @LastEditors: tangyuhui 317972442@qq.com
 * @LastEditTime: 2025-09-10 14:34:33
 */
export function logRequestMsg(data: any,prefix: string='') {
    const eventName = prefix + getUUID(4)
    const config = data && data.config
    const response = data && data.data
    const responseData = (response && response.data) || ''
    const responseCode = (response && response.code) || ''
    const responseMessage = (response && response.message) ||  (response && response.msg) || ''
    const responseTraceId = (response && response.traceId) || ''
    const { url } = config
    const eventArray = url.split('/').slice(3).join('/').split('?')
    // 后端接口名称
    const eventUrl = '/' + eventArray[0]
    // 后端接口id
    const eventId = config && config.errorId
    // 请求参数
    let eventParams = null
    if (config.method === 'get' && config && config.params) {
      eventParams = config && config.params
    } else if (config.method === 'post' && config && config.data) {
      eventParams = isJSONString(config.data) ? JSON.parse(config.data) : config.data
    }
    const desc = eventName + ': ' + responseMessage
    return desc
  }